<?php


namespace App\Http\Controllers\Mini\Circle;


use App\Http\Controllers\Controller;
use App\Models\FleaNotice;
use App\Models\FleaOrder;
use App\Models\FleaOrderComment;
use App\Models\Mini\FleaGoodsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class FleaOrderCommentController extends Controller
{
    private $user_id;

    public function __construct()
    {
        $id = auth('user')->id();
        $this->user_id = $id;
    }

    /**
     * 发布评论
     */
    public function store(Request $request)
    {

        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        $data = $request->post();

        $validator = Validator::make($data, [
            'order_id' => 'required',
            'content' => 'required',
            'images' => 'present'
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }

        $images = array_filter(explode(',', $data['images']), function ($item) {
            return $item && true;
        });

        $data['images'] = json_encode($images);
        $data['user_id'] = $user_id;

        DB::beginTransaction();
        try {
            //检查订单购买人是否是登录的用户
            $order = FleaOrder::query()->where('id', $data['order_id'])->firstOrFail();
            //订单是否待评论
            if ($order['state'] != 3) {
                return $this->error('订单暂时无法评论');
            }
            if ($user_id != $order['user_id']) {
                return $this->error('您不是这个订单的用户');
            }

            //查看商品是哪个用户发布的
            $goods_user_id = FleaGoodsModel::query()->where('id', $order['goods_id'])->value('user_id');
            $data['from_user_id'] = $goods_user_id;

            FleaOrderComment::query()->create($data);
            //订单状态改为交易完成
            FleaOrder::query()->where('id', $data['order_id'])->update(['state' => 4]);
            //记录消息
            FleaNotice::query()->create([
                'type' => 3,  //1收藏  2留言  3订单评论  4提现
                'msg' => '发布了交易评价',
                'order_id' => $data['order_id'],
                'user_id' => $goods_user_id,
                'user_from_id' => $user_id
            ]);
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            return $this->error($exception->getMessage());
        }

        return $this->success();
    }

    //卖家回复接口
    public function replyOrderComment(Request $request)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        $data = $request->post();

        $validator = Validator::make($data, [
            'order_id' => 'required',
            'content' => 'required'
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }

        //卖家ID
        $from_user_id = FleaOrderComment::query()->where('order_id', $data['order_id'])->value('from_user_id');
        if ($user_id != $from_user_id) {
            return $this->error('登录的用户不是卖家');
        }

        $res = FleaOrderComment::query()->where('order_id', $data['order_id'])->update([
            'seller_content' => $data['content'],
            'seller_at' => date('Y-m-d H:i:s')
        ]);

        return $res ? $this->success() : $this->error();
    }

    /**
     * @param $id :订单ID
     * 查看订单下的评论
     */
    public function show($id)
    {
        $data = FleaOrderComment::query()->select('user_id', 'images', 'content', 'seller_content', 'created_at', 'order_id', 'from_user_id', 'seller_at')->where('order_id', $id)->first();
        $data->user;
        $data->seller_user;
        $data->goods;
        return $this->success($data);
    }

    /**
     * 获取我发布的所有评价
     */
    public function getMyComment(Request $request)
    {
        $user_id = $this->user_id;
        if (!$user_id) {
            return $this->error('请先登录');
        }

        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $offset = ($page == 1) ? 0 : ($page - 1) * $limit;

        $data = FleaOrderComment::query()->with('goods:goods_name,goods_images,goods_price')->select('order_id','images', 'content', 'created_at')->where('user_id', $user_id)->offset($offset)->limit($limit)->get();

        $response['data'] = $data;
        $response['count'] = FleaOrderComment::query()->where('user_id', $user_id)->count();
        $response['pages'] = ceil($response['count'] / $limit);
        return $this->success($response);

    }
}
